1.1. Data Types in C
Module 1.1 • Fundamentals of Data Types & Constants
A data type specifies the kind of data that a variable can store in memory. Different types of data require different amounts of memory and support different ranges of values.
When a variable is declared, its data type tells the compiler:
- What kind of value will be stored.
- How much memory should be allocated.
- What operations can be performed on the data.
For example:
int age = 25;
float price = 199.99;
char grade = 'A';
In the above code:
agestores a whole number.pricestores a decimal number.gradestores a single character.
Why Are Data Types Important?
Data types help to:
- Use memory efficiently.
- Improve program performance.
- Prevent invalid operations.
- Increase code readability.
For example, storing a student's age as a character would not be appropriate:
char age = 20; // Not recommended
Instead:
int age = 20; // Correct
Basic Data Types in C
C provides four fundamental data types:
| Data Type | Description | Example |
|---|---|---|
char | Stores a single character | 'A' |
int | Stores whole numbers | 100 |
float | Stores decimal values with single precision | 25.75 |
double | Stores decimal values with higher precision | 3.1415926535 |
1. Integer Type (int)
The int data type is used to store whole numbers without decimal points.
Example
int students = 45;
int temperature = -10;
Output Values
45
-10
Common Uses
- Age
- Quantity
- Marks
- Employee IDs
2. Character Type (char)
The char data type stores a single character enclosed within single quotes.
Example
char grade = 'B';
char symbol = '#';
Common Uses
- Grades
- Gender codes
- Single-letter commands
- Special symbols
3. Floating Point Type (float)
The float data type stores decimal numbers with moderate precision.
Example
float salary = 32500.75;
float temperature = 36.8;
Common Uses
- Prices
- Temperatures
- Measurements
4. Double Type (double)
The double data type stores decimal values with higher precision than float.
Example
double pi = 3.14159265358979;
double interestRate = 7.253468;
Common Uses
- Scientific calculations
- Financial applications
- Engineering computations
Type Modifiers
Type modifiers are used to change the size or range of basic data types. There are two categories:
1. Sign Modifiers
- signed
- unsigned
2. Size Modifiers
- short
- long
Signed and Unsigned Types
Signed
A signed variable can store both positive and negative values.
Example
signed int balance = -2500;
Possible Values
-2500
100
-50
Unsigned
An unsigned variable stores only zero or positive values.
Example
unsigned int population = 500000;
Since no memory is reserved for the sign, unsigned variables can store larger positive values.
Short and Long Types
Short Integer
Used when smaller integer values are expected.
Example
short year = 25;
Long Integer
Used when larger integer values are required.
Example
long distance = 987654321;
Common Data Types and Their Typical Sizes
| Data Type | Typical Size |
|---|---|
| char | 1 byte |
| short int | 2 bytes |
| int | 4 bytes |
| long int | 4 or 8 bytes |
| float | 4 bytes |
| double | 8 bytes |
| long double | 10 to 16 bytes |
Note: Actual sizes may vary depending on the compiler and operating system.
Data Type Example Program
#include <stdio.h>
int main()
{
int quantity = 15;
float price = 249.50;
double tax = 18.123456;
char category = 'E';
printf("Quantity = %d\n", quantity);
printf("Price = %.2f\n", price);
printf("Tax = %.6lf\n", tax);
printf("Category = %c\n", category);
return 0;
}
Output
Quantity = 15
Price = 249.50
Tax = 18.123456
Category = E
Constants in C
A constant is a fixed value that does not change during program execution.
Examples
100
25.75
'A'
"Hello"
Once defined, a constant remains unchanged throughout the program.
Types of Constants
C supports several types of constants:
- Integer Constants
- Real (Floating Point) Constants
- Character Constants
- String Constants
- Symbolic Constants
1.1.1 Numeric Constants
Numeric constants represent numbers.
Rules
- Must contain at least one digit.
- Spaces are not allowed.
- Commas are not allowed.
- May be positive or negative.
Valid Examples
250
-50
7890
Invalid Examples
12,500
35 10
4#5
Reason:
- Commas are not permitted.
- Spaces are not allowed.
- Special characters are not allowed.
Integer Constants
Integer constants are whole numbers without decimal points.
There are three number systems used in C.
Decimal Constants (Base 10)
Uses digits 0–9.
Examples
25
1000
98765
Octal Constants (Base 8)
Uses digits 0–7 only. Must begin with 0.
Examples
012
075
0456
Invalid Example
089
Reason: Digit 8 is not allowed in octal numbers.
Hexadecimal Constants (Base 16)
Uses digits 0–9 and letters A–F. Must begin with 0x or 0X.
Examples
0x10
0x1F
0XABC
0x5D
Integer Constant Suffixes
Suffixes specify the type of an integer constant.
Examples
250U
5000L
75000UL
Meaning:
- U → Unsigned
- L → Long
- UL → Unsigned Long
Real (Floating Point) Constants
Real constants contain a decimal point.
Examples
5.75
0.25
100.0
89.456
Scientific Notation
Very large or very small numbers can be written using exponential notation.
Examples
4.5e6
7.2e-4
1.25E8
Meaning:
4.5 × 10⁶
7.2 × 10⁻⁴
1.25 × 10⁸
Floating Point Suffixes
Examples
2.5F
3.14L
Meaning:
- F → float
- L → long double
1.1.2 Character Constants
A character constant contains exactly one character enclosed in single quotes.
Valid Examples
'A'
'9'
'%'
'k'
Invalid Examples
'AB'
"X"
''
Reasons:
- Only one character is allowed.
- Double quotes create a string.
- Empty character constants are not permitted.
ASCII Values
Every character has a corresponding numeric value called an ASCII code.
Examples
| Character | ASCII Value |
|---|---|
| A | 65 |
| B | 66 |
| a | 97 |
| b | 98 |
| 0 | 48 |
| 9 | 57 |
Example:
char letter = 'A';
Internally, C stores the value 65.
1.1.3 String Constants
A string constant is a sequence of characters enclosed in double quotes.
Examples
"Welcome"
"C Programming"
"2026"
"OpenAI"
Character vs String
'A' : This is a character constant.
"A" : This is a string constant.
The string actually contains:
A\0
where \0 is the null character added automatically by the compiler.
1.1.4 Symbolic Constants
Sometimes a constant value is used repeatedly throughout a program. Instead of writing the value multiple times, we can assign it a name. These are called symbolic constants.
Syntax
#define NAME value
Examples
#define MAX_STUDENTS 100
#define GST 18
#define PI 3.14159265
#define COMPANY "TechZone"
Example Program
#include <stdio.h>
#define PI 3.14159265
int main()
{
float radius = 5;
float area = PI * radius * radius;
printf("Area = %.2f", area);
return 0;
}
Advantages
- Improves readability.
- Makes programs easier to maintain.
- Reduces repeated values.
- Simplifies updates.
Derived Data Types
Derived data types are built using basic data types. They include:
- Arrays
- Pointers
- Structures
- Unions
Array
Stores multiple values of the same type.
int marks[5] = {80, 75, 90, 85, 88};
Pointer
Stores the memory address of another variable.
int num = 10;
int *ptr = #
Structure
Groups variables of different data types under one name.
struct Student
{
char name[30];
int age;
};
Union
Allows different data types to share the same memory location.
union Data
{
int number;
float price;
};
Enumeration (enum)
An enumeration is a user-defined data type containing named integer constants.
Example
enum Month
{
JAN,
FEB,
MAR
};
Void Data Type
The void data type represents the absence of a value.
Example
void displayMessage()
{
printf("Welcome");
}
A function with void does not return any value.
Summary
- Data types define the kind of data stored in variables.
- The four basic data types are int, char, float, and double.
- Modifiers such as signed, unsigned, short, and long alter size and range.
- Constants represent fixed values that do not change during execution.
- C supports integer, real, character, string, and symbolic constants.
- Arrays, pointers, structures, and unions are derived data types.
- enum improves readability using named constants.
- void represents the absence of a value.
- Choosing the correct data type improves memory usage, performance, and program reliability.
Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.